﻿@{
    ViewBag.TitlePrefix = "Archive ";
}

@using Moonglade.Core
@using System.Globalization
@model IReadOnlyList<Moonglade.Model.Archive>

@{
    int[] yearList = null;
    if (null != Model && Model.Any())
    {
        yearList = (from item in Model
                    orderby item.Year descending
                    select item.Year).Distinct().ToArray();
    }

    static string GetMonthNameByNumber(int number)
    {
        if (number > 12 || number < 1)
        {
            return string.Empty;
        }

        return CultureInfo.GetCultureInfo("en-US").DateTimeFormat.GetMonthName(number);
    }
}

@if (null != ViewBag.IsServerError && ViewBag.IsServerError)
{
    <partial name="_ServerErrorMessage" />
}
else
{
    <h3>
        @Localizer["Archive Timeline"]
    </h3>
    <hr />

    @if (null != Model && Model.Any() && null != yearList)
    {
        <ul class="timeline" id="monthList">
            @foreach (var item in yearList)
            {
                var queryMonth = Model.Where(obj => obj.Year == item);
                var yearEndChar = item % 10;
                var badgeClass = (yearEndChar % 5) switch
                {
                    0 => "primary",
                    1 => "success",
                    2 => "info",
                    3 => "warning",
                    4 => "danger",
                    _ => string.Empty
                };

                <li class="@(item % 2 == 0 ? "timeline-inverted" : null)">
                    <div class="timeline-badge @badgeClass"><span class="icon-clock"></span></div>
                    <div class="timeline-panel">
                        <div class="timeline-heading">
                            <h3 class="timeline-title">
                                <a asp-controller="Archive" asp-action="List" asp-route-year="@item">@item</a>
                            </h3>
                        </div>
                        <div class="timeline-body">
                            <div class="row">
                                @foreach (var month in queryMonth)
                                {
                                    <div class="col-md-6 col-sm-6 col-6">
                                        <span class="badge badge-secondary float-right">@(month.Count)</span>
                                        <a asp-controller="Archive" asp-action="List"
                                           asp-route-year="@month.Year"
                                           asp-route-month="@month.Month">
                                            @GetMonthNameByNumber(month.Month)
                                        </a>
                                    </div>
                                }
                            </div>
                        </div>
                    </div>
                </li>
            }
        </ul>
    }
    else
    {
        <div class="alert alert-info">@Localizer["No Archive"]</div>
    }
}

<partial name="_LightSwitch" />